# -*- coding: utf-8 -*-
import json
from copy import deepcopy
from flask import Blueprint, request, jsonify
from controllers.common import *
from controllers.db import *

match_numerical_conf_api = Blueprint('match_numerical_conf_api', __name__)
match_numerical_conf_down_api = Blueprint('match_numerical_conf_down_api', __name__)

@match_numerical_conf_api.route("/match_numerical_conf", methods=['POST'])
def project_action():
    update_data = json.loads(request.data)
    _type = update_data['type']

    result = {'msg': '成功', 'code': 200, 'data':[], 'ret': 0}

    project_data = MatchNumericalConf.query.first()

    if _type == 'ava_score_difference':
        ava = json.loads(project_data.ava)
        score_difference = ava['score_difference']
        for i in range(len(score_difference)):
            score_difference[i]['idx'] = i

        result['score_difference'] = score_difference

    elif _type == 'update_ava_score_difference':
        score_difference = update_data['score_difference']
        for node in score_difference:
            if 'idx' in node:
                del node['idx']
        ava = json.loads(project_data.ava)
        ava['score_difference'] = score_difference
        project_data.ava = json.dumps(ava)
        db.session.commit()

    elif _type == 'ava_win_rate_difference':
        ava = json.loads(project_data.ava)
        win_rate_difference = ava['win_rate_difference']
        for i in range(len(win_rate_difference)):
            win_rate_difference[i]['idx'] = i

        result['win_rate_difference'] = win_rate_difference

    elif _type == 'update_ava_win_rate_difference':
        win_rate_difference = update_data['win_rate_difference']
        for node in win_rate_difference:
            if 'idx' in node:
                del node['idx']
        ava = json.loads(project_data.ava)
        ava['win_rate_difference'] = win_rate_difference
        project_data.ava = json.dumps(ava)
        db.session.commit()

    elif _type == 'ava_near_enemy':
        ava = json.loads(project_data.ava)
        near_enemy = ava['near_enemy']
        result['near_enemy'] = near_enemy

    elif _type == 'update_ava_near_enemy':
        near_enemy = update_data['near_enemy']
        ava = json.loads(project_data.ava)
        ava['near_enemy'] = near_enemy
        project_data.ava = json.dumps(ava)
        db.session.commit()

    elif _type == 'legion_score_difference':
        legion = json.loads(project_data.legion)
        score_difference = legion['score_difference']
        for i in range(len(score_difference)):
            score_difference[i]['idx'] = i

        result['score_difference'] = score_difference

    elif _type == 'update_legion_score_difference':
        score_difference = update_data['score_difference']
        for node in score_difference:
            if 'idx' in node:
                del node['idx']
        legion = json.loads(project_data.legion)
        legion['score_difference'] = score_difference
        project_data.legion = json.dumps(legion)
        db.session.commit()

    elif _type == 'legion_win_rate_difference':
        legion = json.loads(project_data.legion)
        win_rate_difference = legion['win_rate_difference']
        for i in range(len(win_rate_difference)):
            win_rate_difference[i]['idx'] = i

        result['win_rate_difference'] = win_rate_difference

    elif _type == 'update_legion_win_rate_difference':
        win_rate_difference = update_data['win_rate_difference']
        for node in win_rate_difference:
            if 'idx' in node:
                del node['idx']
        legion = json.loads(project_data.legion)
        legion['win_rate_difference'] = win_rate_difference
        project_data.legion = json.dumps(legion)
        db.session.commit()

    elif _type == 'legion_near_enemy':
        legion = json.loads(project_data.legion)
        near_enemy = legion['near_enemy']
        result['near_enemy'] = near_enemy

    elif _type == 'update_legion_near_enemy':
        near_enemy = update_data['near_enemy']
        legion = json.loads(project_data.legion)
        legion['near_enemy'] = near_enemy
        project_data.legion = json.dumps(legion)
        db.session.commit()


    elif _type == 'new_legion_score_difference':
        legion = json.loads(project_data.new_legion)
        score_difference = legion['score_difference']
        for i in range(len(score_difference)):
            score_difference[i]['idx'] = i

        result['score_difference'] = score_difference

    elif _type == 'update_new_legion_score_difference':
        score_difference = update_data['score_difference']
        for node in score_difference:
            if 'idx' in node:
                del node['idx']
        legion = json.loads(project_data.new_legion)
        legion['score_difference'] = score_difference
        project_data.new_legion = json.dumps(legion)
        db.session.commit()

    elif _type == 'new_legion_win_rate_difference':
        legion = json.loads(project_data.new_legion)
        win_rate_difference = legion['win_rate_difference']
        for i in range(len(win_rate_difference)):
            win_rate_difference[i]['idx'] = i

        result['win_rate_difference'] = win_rate_difference

    elif _type == 'update_new_legion_win_rate_difference':
        win_rate_difference = update_data['win_rate_difference']
        for node in win_rate_difference:
            if 'idx' in node:
                del node['idx']
        legion = json.loads(project_data.new_legion)
        legion['win_rate_difference'] = win_rate_difference
        project_data.new_legion = json.dumps(legion)
        db.session.commit()

    elif _type == 'new_legion_near_enemy':
        legion = json.loads(project_data.new_legion)
        near_enemy = legion['near_enemy']
        result['near_enemy'] = near_enemy

    elif _type == 'update_new_legion_near_enemy':
        near_enemy = update_data['near_enemy']
        legion = json.loads(project_data.new_legion)
        legion['near_enemy'] = near_enemy
        project_data.new_legion = json.dumps(legion)
        db.session.commit()

    return jsonify(result) 

@match_numerical_conf_down_api.route("/match_numerical_conf_down/<int:is_push>", methods=['POST', 'GET'])
def DownAction(is_push):
    result = {}

    query = MatchNumericalConf.query.first()
    ava = json.loads(query.ava)
    legion = json.loads(query.legion)
    new_legion = json.loads(query.new_legion)

    ava_score_difference = ava['score_difference']
    ava_win_rate_difference = ava['win_rate_difference']
    ava_near_enemy = ava['near_enemy']
    new_ava_score = []
    new_ava_win = []

    for node in ava_score_difference:
        new_ava_score.append({'rate': [node['from'], node['to']],
                              'a': node['a']})

    # 20220726 score_difference 最后一个的to 默认为-1 无穷大
    new_ava_score[-1]['rate'][-1] = -1

    for node in ava_win_rate_difference:
        new_ava_win.append({'rate': [node['from'], node['to']],
                            'b': node['b']})

    new_ava_near = {'battle_num': ava_near_enemy['battle_num'],
                    'c': [ava_near_enemy['c_from'],
                          ava_near_enemy['c_to']]}

    result['ava'] = {'score_difference': new_ava_score,
                     'win_rate_difference': new_ava_win,
                     'near_enemy': new_ava_near}

    # legion
    legion_score_difference = legion['score_difference']
    legion_win_rate_difference = legion['win_rate_difference']
    legion_near_enemy = legion['near_enemy']
    new_legion_score = []
    new_legion_win = []

    for node in legion_score_difference:
        new_legion_score.append({'rate': [node['from'], node['to']],
                                 'a': node['a']})

    # 20220726 score_difference 最后一个的to 默认为-1 无穷大
    new_legion_score[-1]['rate'][-1] = -1

    for node in legion_win_rate_difference:
        new_legion_win.append({'rate': [node['from'], node['to']],
                               'b': node['b']})

    new_legion_near = {'battle_num': legion_near_enemy['battle_num'],
                       'c': [legion_near_enemy['c_from'],
                             legion_near_enemy['c_to']]}

    result['legion'] = {'score_difference': new_legion_score,
                        'win_rate_difference': new_legion_win,
                        'near_enemy': new_legion_near}

    # 新legion
    new_legion_score_difference = new_legion['score_difference']
    new_legion_win_rate_difference = new_legion['win_rate_difference']
    new_legion_near_enemy = new_legion['near_enemy']
    new_new_legion_score = []
    new_new_legion_win = []

    for node in new_legion_score_difference:
        new_new_legion_score.append({'rate': [node['from'], node['to']],
                                 'a': node['a']})

    # 20220726 score_difference 最后一个的to 默认为-1 无穷大
    new_new_legion_score[-1]['rate'][-1] = -1

    for node in new_legion_win_rate_difference:
        new_new_legion_win.append({'rate': [node['from'], node['to']],
                               'b': node['b']})

    new_new_legion_near = {'battle_num': new_legion_near_enemy['battle_num'],
                       'c': [new_legion_near_enemy['c_from'],
                             new_legion_near_enemy['c_to']]}

    result['new_legion'] = {'score_difference': new_new_legion_score,
                        'win_rate_difference': new_new_legion_win,
                        'near_enemy': new_new_legion_near}

    response = DownLoad('match_numerical_conf', result, is_push)

    return response
